function i_cmp(r1, nr, skip, spikethresh, varargin)
%
% function to be called from the datac command line.
% Will make a new figure if not already existing called
% i_cmp.
% the current (I) data will be culled from datac
% and the records r1:skip:(r1+nr) averaged and plotted
% subsequent calls add to current display.
% A call with no parameters clears the current display.
% a call with more parameters displays the data in multiple subplots
% with the same parameters as the first display.
% 8/28/2000 P. Manis.
% For current: 11/27/2000
%
h = findobj('Tag', 'i_cmp'); % look for the figure
if(isempty(h))
   h = figure;
   set(h, 'Tag', 'i_cmp');
   set(h, 'Name', 'I Compare');
end;
figure(h);
if(nargin == 0)
   clf;
   set(h, 'UserData', []);
   return;
end;

v = datac('geti');
d = datac('getdfile');
c = datac('sel');
rl = d.record;
np=1+length(varargin);
allrs = [r1 varargin{:}]

if(length(varargin) > 0 & varargin{end} == '+') % last argument is + for append
   allrs = [r1 varargin{1:end-1}];
else
   clf;
   h = findobj('Tag', 'i_cmp'); % look for the figure
   set(h, 'UserData', []);
end;

nc = get(h,'UserData');
if(isempty(nc)) nc = 0; end;
tb = make_time(d);
tb = tb(1,:);
c.psp_time(1)=c.psp_time(1)+0.75;
c.psp_time(3)=c.psp_time(6)-2;
x=floor(c.psp_time/(d.rate(1)*0.001*2));
xl=[1:x(1) x(2):x(3) x(6):x(7)]; % that gets the psp windows baseline and spikes...

color=['k', 'r', 'b', 'g', 'c', 'y', 'k'];
nrec = [];
blmax=x(1);
np=0;
for rs = allrs
   [rln, k] = intersect(rl, [rs:skip:(rs+nr)]);
   rln;
   if(isempty(rln))
      fprintf(2, 'Records %d-%d not found in current dataset\n', r1, r1+nr);
      return;
   end;
   u = any(v(k,xl)' > spikethresh); % check all windows simultaneously for spikes
   fprintf(2, '%d of %d records have currents > %6.1f\n', length(find(u==1)), length(k), spikethresh);
   kx=k(u==0);	% exclude traces with points outside the window...
   b=zeros(1, max(kx));
   if(length(kx) > 1)
      % offset data from baseline
      b(kx)=mean(v(kx, 1:blmax)');
      for i = 1:length(kx)
         vo(kx(i),:)=v(kx(i),:)-b(kx(i));
      end;
      np=np+1;
      va=mean(vo(kx,:));
      vs=std(vo(kx,:));
      vstd(np)=mean(vs);
      vp=va+vs;
      vn=va-vs;
      vpat(np,:) = [vp vn(end:-1:1)];
      vd(np,:) = va;
   end;
   nc = nc + 1;
   nrec(nc) = length(kx);
end;
tpat = [tb tb(end:-1:1)];
[s, u] = sort(vstd); % sort standard deviations in reverse order
u = fliplr(u); % reverse
colormap(colorcube);
v=colormap;
vw = brighten(v, 1.5);
for i = u
   hold on;
   w = i; %rem((i-1)*4+1, 64)
   patch(tpat, vpat(i,:), vw(w,:), 'EdgeColor', 'none');
end;
for i = 1:np
   w = i; %rem((i-1)*4+1, 64);
   plot(tb(1,:), vd(i,:), 'color', v(w,:), 'LineWidth', lwx(nc, 2), 'erasemode', 'xor');
end;

return;

function [w] = lwx(nc, i)
w = 0.333*(rem(nc, i)+1);
return;

function [b] = brighten(d, f)
%
b = d*f;
j=find(b > 1);
b(j)=1;
return;

